﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;

using TempEmployees.DAL;

namespace TempEmployees.Web
{
    using UserList = Dictionary<string, User>;
    using TempEmployees.BLL.Database;
    using TempEmployees.BLL.EntitiesExtensions; 

    public partial class UserBudgets : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetUsersDropDown();
            }

            BudgetExistsLabel.Visible = false;
        }

        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (BudgetsList.SelectedIndex == -1 && BudgetsList.Items.Count > 0) BudgetsList.SelectedIndex = 0;
            if (UsersList.SelectedIndex == -1 && UsersList.Items.Count > 0) UsersList.SelectedIndex = 0;
            SetStatusLabel();
        }

        protected void NewBudgetTextBox_Click(object sender, EventArgs e)
        {
            string budgetId = NewBudgetTextBox.Text;
            if (string.IsNullOrEmpty(budgetId)) return;
            if (!DbQuery.Insert<Budget>(new Budget(budgetId, int.Parse(BudgetTypeList.SelectedValue))))
            {
                BudgetExistsLabel.Visible = true;
                return;
            }
            BudgetExistsLabel.Visible = false;
            NewBudgetTextBox.Text = string.Empty;
            BudgetsList.DataBind();
        }


        protected void BudgetsList_DataBound(object sender, EventArgs e)
        {
            Page_LoadComplete(this, new EventArgs());
        }

        protected void AssignBudgetButton_Click(object sender, EventArgs e)
        {
            EBudget.AssignBudget(UsersList.SelectedValue, BudgetsList.SelectedValue, int.Parse(BudgetTypeList.SelectedValue));
            SetStatusLabel();
        }

        protected void UsersDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetStatusLabel();
        }

        protected void BudgetsList_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetStatusLabel();
        }

        protected void SetStatusLabel()
        {
            string budgetId = BudgetsList.SelectedValue;
            string userId = UsersList.SelectedValue;

            bool assigned;
            using (TempEmpDatabaseDataContext db = DbServices.db) 
                assigned = (db.AssignedBudgets.Where(b => b.UserID == userId && b.BudgetID == budgetId).Count() > 0);

            LabelStatus.Text = assigned ? "תקציב שייך למעסיק" : "תקציב לא שייך למעסיק";
            AssignBudgetButton.Visible = !assigned;
        }

        protected void SetUsersDropDown()
        {
            using (TempEmpDatabaseDataContext db = DbServices.db)
            {
                var users =
                    (from u in DbServices.db.Users
                     where (u.Roles & (int)DbConstants.Roles.Employer) == (int)DbConstants.Roles.Employer
                     select new ListItem
                     {
                         Text = u.ToString(),
                         Value = u.ID
                     });
                UsersList.Items.AddRange(users.ToArray());
            }
        }
    }
}